package com.yq.cp.service.yw.impl;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yq.cp.entity.yw.YwDxfsjl;
import com.yq.cp.mapper.yw.YwDxfsjlMapper;
import com.yq.cp.service.yw.YwDxfsjlService;
import com.yq.cp.utils.DateTimeUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 运维-短信发送记录 服务实现类
 * </p>
 *
 * @author zh
 * @since 2024-10-16
 */
@Service
public class YwDxfsjlServiceImpl extends ServiceImpl<YwDxfsjlMapper, YwDxfsjl> implements YwDxfsjlService {
//    LTAI5tPvy3SiYx13ofi2CeYm
//            NBieZVKpUYUVcYmJOoBXzt7Wd4ud2p
    private static String sendNote="dysmsapi.aliyuncs.com";//发送短信
    private static String accessKeYId="LTAI5tPvy3SiYx13ofi2CeYm";//
    private static String accessSecret="NBieZVKpUYUVcYmJOoBXzt7Wd4ud2p";//
    private static String signName="能控动力技术";//
    private static String templateCode1="SMS_474110437";//设备故障断链告警
    private static String templateCode2="SMS_474215433";//设备超过阈值短信告警

    @Resource
    YwDxfsjlMapper ywDxfsjlMapper;
    @Override
    @Transactional
    public void send(YwDxfsjl ywDxfsjl) {
        String phone = ywDxfsjl.getPhone();
        QueryWrapper<YwDxfsjl> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        queryWrapper.eq("dxmb",ywDxfsjl.getDxmb());
        queryWrapper.eq("sbbm",ywDxfsjl.getSbbm());
        queryWrapper.ge("create_time",DateTimeUtil.frontDateTime(60));

        Long aLong = ywDxfsjlMapper.selectCount(queryWrapper);
        if (aLong>1){
            return;
        }
        String gzdx="";
        if (ywDxfsjl.getDxmb().equals(templateCode1)){
            gzdx = "{\"zd\":\"" + ywDxfsjl.getZdmc() + "\",\"sbmc\":\"" + ywDxfsjl.getSbmc() + "\",\"sbid\":\"" + ywDxfsjl.getSbbm() + "\"}";
        }else {
            gzdx = "{\"zd\":\"" + ywDxfsjl.getZdmc() + "\",\"sbmc\":\"" + ywDxfsjl.getSbmc() + "\",\"sbid\":\"" + ywDxfsjl.getSbbm() + "\",\"yz\":\"" + ywDxfsjl.getYz()+ "\"}";
        }


        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(sendNote);
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", ywDxfsjl.getDxmb());
        request.putQueryParameter("TemplateParam", gzdx);

        try {
            DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeYId, accessSecret);
            IAcsClient client = new DefaultAcsClient(profile);
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(JSON.toJSONString(response));
            ywDxfsjlMapper.insert(ywDxfsjl);
        } catch (Exception e) {
            e.getStackTrace();
        }
    }

    public static void main(String[] args) {
        YwDxfsjl ywDxfsjl=new YwDxfsjl();
        ywDxfsjl.setPhone("13647631547");
        ywDxfsjl.setDxmb(templateCode1);
        ywDxfsjl.setZdmc("重庆能源站");
        ywDxfsjl.setSbmc("逆变器");
        ywDxfsjl.setSbbm("NBQ-001");

        YwDxfsjlServiceImpl ywDxfsjlService=new YwDxfsjlServiceImpl();
        ywDxfsjlService.send(ywDxfsjl);
        ywDxfsjl.setDxmb(templateCode2);
        ywDxfsjl.setYz("100kwh");
        ywDxfsjlService.send(ywDxfsjl);
    }
}
